package com.it.advice;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.util.SaResult;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobExceptionHandler {

    @ExceptionHandler(NotLoginException.class)
    public SaResult noLogin(NotLoginException exception) {
        exception.printStackTrace();
        return SaResult.error(exception.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public SaResult runtimeException(RuntimeException ex) {
        ex.printStackTrace();
        return SaResult.error(ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public SaResult exception(Exception ex) {
        return SaResult.error(ex.getMessage());
    }

    @ExceptionHandler(RedisConnectionFailureException.class)
    public SaResult redisConnectionFailureException(RedisConnectionFailureException rex) {
        return SaResult.error("后端缓存连接失败!");
    }

    @ExceptionHandler(NotPermissionException.class)
    public SaResult permissionException(NotPermissionException ex) {
        return SaResult.error("权限校验失败!");
    }
}
